﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DynaSpace.Entity;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using DynaSpace.Menu;

namespace DynaSpace.Game1
{
	public class ComponentClass : DynaComponentBase
	{
		public PlayerView pv;
		public PlayerView pv2;

		internal static Texture2D texBomb;

		public ComponentClass(DynaGame game)
			:base(game)
		{

		}

		protected override void LoadContent()
		{
			base.LoadContent();

			// načíst textury
			texBomb = Game.Content.Load<Texture2D>("gfx\\bomb");
		}

		public override void Initialize()
		{
			base.Initialize();

			// vygenerovat mapu
			Playground = new Playground(50, 50);
			MapGenerator.GenerateMap(Playground);

			// odchytavat udalost smrti
			Playground.Players.ForEach(p => p.Dead += new EventHandler(p_Dead));

			// vytvorit pohledy na hraci plochy
			int w = Game.GraphicsDevice.PresentationParameters.BackBufferWidth;
			int h = Game.GraphicsDevice.PresentationParameters.BackBufferHeight;

			pv = new PlayerView(0, 0, w, h/2, Playground.Players[1], Playground, PlayerView.ScrollingSpeed.SlowFollow);
			pv2 = new PlayerView(0,h/2+1, w, h/2-1, Playground.Players[0], Playground, PlayerView.ScrollingSpeed.SpeedFollow);

			// vytvorit ovladani hracu
			Playground.Players[0].Engine = new PlayerEngineGame1(Playground);
			Playground.Players[1].Engine = new PlayerEngineGame1(Playground);
		}

		void p_Dead(object sender, EventArgs e) // pokud hrac umre
		{
			Player player = sender as Player;
			player.Delete();
			Menu.MenuContainer deadMenu = new DynaSpace.Menu.MenuContainer("dead", true);
			deadMenu.Items.Add(new MenuHeader("Utřel hráč " + player.Index.ToString() + "!"));
			deadMenu.Items.Add(new MenuButton("Njn, ok...", new Action(()=> { Game.Menus.Items.Remove(deadMenu); player.AddAgain(); })));
			deadMenu.Center();
			deadMenu.SetFocusToFirstControl();
			Game.Menus.Items.AddLast(deadMenu);
		}

		public override void Update(GameTime gameTime)
		{
			// upravit pohled, aby zabíral hráče
			pv2.Update(gameTime);
			pv.Update(gameTime);
		}

		public override void Draw(GameTime gameTime)
		{
			pv.Render(spriteBatch);
			pv2.Render(spriteBatch);
		}
	}

}
